package com.pdf.editor;

import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.VBox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Objects;

public class PDFTab extends Tab {

    private File file;
    private float scale = 2.6f;

    public PDFTab(String title, File file) {
        super(title);

        this.file = file;
        this.setClosable(true);
        init(scale);
    }


    public void init(float scale) {

        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        try (PDDocument document = PDDocument.load(file)) {
            PDFRenderer renderer = new PDFRenderer(document);
            for (int i = 0; i < document.getPages().getCount(); i++) {
                BufferedImage bufferedImage = renderer.renderImage(i, scale);
                ImageView imageView = new ImageView(SwingFXUtils.toFXImage(bufferedImage, null));
                imageView.setFitWidth(bufferedImage.getWidth());
                imageView.setFitHeight(bufferedImage.getHeight());
                box.getChildren().add(imageView);
            }
            mouseScrollEvent(box);

            ScrollPane scrollPane = new ScrollPane(box);
            scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
            scrollPane.setFitToWidth(true);
            scrollPane.setFitToHeight(true);
            scrollPane.getStylesheets().add(Objects.requireNonNull(getClass().getResource("scroll-bar.css")).toExternalForm());

            this.setContent(scrollPane);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void mouseScrollEvent(VBox vBox) {

        vBox.addEventHandler(ScrollEvent.SCROLL, event -> {
            if (event.isControlDown()) {
                if (event.getDeltaY() > 0) {
                    vBox.setScaleX(vBox.getScaleX() * 1.1);
                    vBox.setScaleY(vBox.getScaleY() * 1.1);
                } else {
                    vBox.setScaleX(vBox.getScaleX() / 1.1);
                    vBox.setScaleY(vBox.getScaleY() / 1.1);
                }
            }
        });

        /*this.getContent().setOnScroll(event -> {

            if (event.isControlDown() && event.getDeltaY() > 0) {
                if (scale < 4)
                    init(scale += 0.2f);
            }
            if (event.isControlDown() && event.getDeltaY() < 0) {
                if (scale > 1)
                    init(scale -= 0.2f);
            }
        });*/

    }

    public InputStream parseStream(BufferedImage bufferedImage) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpeg", byteArrayOutputStream);

        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }


}
